# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'pagetest.ui'
#
# Created: Mon Mar 16 14:24:03 2009
#      by: PyQt4 UI code generator 4.3.3
#
# WARNING! All changes made in this file will be lost!



import os
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import Qt
from PyQt4.QtGui import QWidget
from DAC.BatimentDAC import Batiment

globals()["x"] = 0
globals()["y"] = 0
class Page2(QtGui.QMainWindow):
    
    def suivant(self):
	error = 0
        self.dicobatiment["x"]=str(0)
        self.dicobatiment["y"]=str(0)
        self.dicobatiment["z"]=str(0)
        self.dicobatiment["realheight"]=self.lineEdit_longueur_vide.displayText()
        self.dicobatiment["realwidth"]=self.lineEdit_largeur_vide.displayText()
        self.dicobatiment["nom"]=self.lineEdit_nom_vide.displayText()
        self.dicobatiment["description"]=self.textEdit_description_vide.toPlainText()
	self.dicobatiment["previousheight"] = str(self.objImg.height())
        self.dicobatiment["previouswidth"] = str(self.objImg.width())
        self.dicobatiment["nextheight"] = str(self.objImgModif.height())
        self.dicobatiment["nextwidth"] = str(self.objImgModif.width())
	self.dicobatiment["url_plan"]=self.filename

        if ((self.lineEdit_longueur_vide.displayText() == '') or (self.lineEdit_longueur_vide.displayText() == "cm")):
		self.lineEdit_longueur_vide.setText("cm")
		error = error + 1
	if ((self.lineEdit_largeur_vide.displayText() == '') or (self.lineEdit_largeur_vide.displayText() == "cm")):
	        self.lineEdit_largeur_vide.setText("cm")
		error = error + 1
        if ((self.lineEdit_nom_vide.displayText() == '') or (self.lineEdit_nom_vide.displayText() == "le nom du batiment ici...")):
        	self.lineEdit_nom_vide.setText("le nom du batiment ici...")
		error = error + 1
        if ((self.textEdit_description_vide.toPlainText() == '') or (self.textEdit_description_vide.toPlainText() == "la description du batiment ici...")):
        	self.textEdit_description_vide.setText("la description du batiment ici...")
		error = error + 1
	if (self.filename == ''):
		error = error + 1

	if (error == 0):
		index=self.comboBox_site_existant.currentIndex()
		if(index >= 2):
		        #print self.list[index-2]["ID"]
			batiment=Batiment(self.list[index-2]["ID"])
			batiment.createByDico(self.dicobatiment)
			batiment.updateIntoDb()
		else:   
			batiment=Batiment()
			batiment.createByDico(self.dicobatiment)
			batiment.insertToDb()

		fichier=open('dicobatiment','a')
		fichier.write('\nnom:'+self.lineEdit_nom_vide.displayText())
		fichier.write('\nx:'+str(globals()["x"]))
		fichier.write('\ny:'+str(globals()["y"]))
		fichier.write('\nrealheight:'+self.lineEdit_longueur_vide.displayText())
		fichier.write('\nrealwidth:'+self.lineEdit_largeur_vide.displayText())
		fichier.write('\nurl_plan:'+self.filename)
		fichier.write('\nid:'+batiment.id["value"])
		fichier.write('\npreviousheight:'+str(self.objImg.height()))
		fichier.write('\npreviouswidth:'+str(self.objImg.width()))
		fichier.write('\nnextheight:'+str(self.objImgModif.height()))
		fichier.write('\nnextwidth:'+str(self.objImgModif.width()))
		fichier.close()

        	### ajouter dans gestion###
        	self.window.close()
        	os.system("./launch_page3.py")



    def parcourir(self):
 
	# On récupère le chemin de l'image à récupérer
	self.filename = str(QtGui.QFileDialog.getOpenFileName(self, 'Open file', '/home'))
 
	# test pour vérifier que le chemin à bien été récupéré	
	b="echo "+self.filename	
	os.system(b)
       
        # création d'un objet image pour pouvoir la manipuler ensuite
        self.objImg = QtGui.QImage(self.filename)

	# redimensionnement de l'image
	self.objImgModif = self.objImg.scaled(530,460,Qt.KeepAspectRatio,Qt.FastTransformation)

	# taille de l'image
	a = self.objImgModif.height()
	b = self.objImgModif.width()
        
	# redimensionnement du widget
	self.repere_coord.resize(b,a)

        # affichage de l'objet image dans le label
        self.plan.clear()
	self.plan.setGeometry(QtCore.QRect(0,0,b,a))
	self.plan.setPixmap(QtGui.QPixmap.fromImage(self.objImgModif))
	self.repere.setVisible(True)


    def displayBatiment(self):

      index=self.comboBox_site_existant.currentIndex()
     
      if index >= 2:
           self.lineEdit_nom_vide.setText(self.list[index-2]["nom"])
           self.textEdit_description_vide.setText(self.list[index-2]["description"])
           self.lineEdit_longueur_vide.setText(str(self.list[index-2]["realheight"]))
           self.lineEdit_largeur_vide.setText(str(self.list[index-2]["realwidth"]))
           self.textEdit_ref_relative_vide.setText("x:"+str(self.list[index-2]["x"])+"\ny:"+str(self.list[index-2]["y"])+"\nz:"+str(self.list[index-2]["z"]))
           
           self.filename= self.list[index-2]["url_plan"]
           
           self.objImg = QtGui.QImage(self.filename) 
           self.objImgModif = self.objImg.scaled(530,460,Qt.KeepAspectRatio,Qt.FastTransformation)
	   a = self.objImgModif.height()
	   b = self.objImgModif.width()
	   
	   
	   self.repere_coord.resize(b,a)

        # affichage de l'objet image dans le label
           self.plan.clear()
	   self.plan.setGeometry(QtCore.QRect(0,0,b,a))
	   self.plan.setPixmap(QtGui.QPixmap.fromImage(self.objImgModif))
	   self.repere.setVisible(True)
      else:
           self.lineEdit_nom_vide.setText("le nom du batiment ici...")
           self.textEdit_description_vide.setText("la description du batiment ici...")
           self.lineEdit_longueur_vide.setText("cm")
           self.lineEdit_largeur_vide.setText("cm")
           self.textEdit_ref_relative_vide.setText("")
           
           self.filename= "../images/vide.jpg"
           
           self.objImg = QtGui.QImage(self.filename) 
           self.objImgModif = self.objImg.scaled(530,460,Qt.KeepAspectRatio,Qt.FastTransformation)
	   a = self.objImgModif.height()
	   b = self.objImgModif.width()
	   
	   
	   self.repere_coord.resize(b,a)

        # affichage de l'objet image dans le label
           self.plan.clear()
	   self.plan.setGeometry(QtCore.QRect(0,0,b,a))
	   self.plan.setPixmap(QtGui.QPixmap.fromImage(self.objImgModif))
	   self.repere.setVisible(False)
     #url + x,y,z
      


    def precedent(self):
       self.window.close()
       os.system("./Main.py 1")
  
     
    def setupUi(self, Dialog):
             
        self.list=[]

        self.dicobatiment={}
        os.system('echo>dicobatiment')
        self.window=Dialog
	self.filename = ""
        Dialog.setObjectName("Dialog")
        Dialog.resize(QtCore.QSize(QtCore.QRect(0,0,1251,719).size()).expandedTo(Dialog.minimumSizeHint()))

        #Longueur
        self.textEdit_verticale_longueur = QtGui.QTextEdit(Dialog)
        self.textEdit_verticale_longueur.setGeometry(QtCore.QRect(658, 250, 23, 190))
        sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Expanding)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.textEdit_verticale_longueur.sizePolicy().hasHeightForWidth())
        

	self.textEdit_verticale_longueur.setSizePolicy(sizePolicy)
        font = QtGui.QFont()
        font.setFamily("Bitstream Charter")
        font.setPointSize(12)
        self.textEdit_verticale_longueur.setFont(font)
        self.textEdit_verticale_longueur.setFrameShape(QtGui.QFrame.VLine)
        self.textEdit_verticale_longueur.setReadOnly(True)
        self.textEdit_verticale_longueur.setObjectName("textEdit_verticale_longueur")
        self.lineEdit_site = QtGui.QLineEdit(Dialog)
        self.lineEdit_site.setGeometry(QtCore.QRect(440,14,370,71))

        #Largeur
        self.textEdit_horizontal_largeur = QtGui.QTextEdit(Dialog)
        self.textEdit_horizontal_largeur.setGeometry(QtCore.QRect(880, 107, 110, 27))
        font = QtGui.QFont()
        font.setFamily("Bitstream Charter")
        font.setPointSize(12)
        self.textEdit_horizontal_largeur.setFont(font)
        self.textEdit_horizontal_largeur.setReadOnly(True)
        self.textEdit_horizontal_largeur.setObjectName("textEdit_horizontal_largeur")

        font = QtGui.QFont()
        font.setPointSize(32)
        font.setWeight(50)
        font.setBold(False)
        self.lineEdit_site.setFont(font)
        self.lineEdit_site.setAlignment(QtCore.Qt.AlignCenter)
        self.lineEdit_site.setReadOnly(True)
        self.lineEdit_site.setObjectName("lineEdit_site")

        self.pushButton_suivant = QtGui.QPushButton(Dialog)
        self.pushButton_suivant.setGeometry(QtCore.QRect(1064,650,171,41))

        font = QtGui.QFont()
        font.setPointSize(12)
        self.pushButton_suivant.setFont(font)
        self.pushButton_suivant.setObjectName("pushButton_suivant")

        self.pushButton_annuler = QtGui.QPushButton(Dialog)
        self.pushButton_annuler.setGeometry(QtCore.QRect(850,650,171,41))

        font = QtGui.QFont()
        font.setPointSize(12)
        self.pushButton_annuler.setFont(font)
        self.pushButton_annuler.setObjectName("pushButton_annuler")

        self.groupe_informations = QtGui.QWidget(Dialog)
        self.groupe_informations.setGeometry(QtCore.QRect(10,100,651,591))
        self.groupe_informations.setObjectName("groupe_informations")

        self.lineEdit_site_existant = QtGui.QLineEdit(self.groupe_informations)
        self.lineEdit_site_existant.setGeometry(QtCore.QRect(10,10,291,41))

        font = QtGui.QFont()
        font.setPointSize(16)
        font.setUnderline(True)
        self.lineEdit_site_existant.setFont(font)
        self.lineEdit_site_existant.setAlignment(QtCore.Qt.AlignCenter)
        self.lineEdit_site_existant.setReadOnly(True)
        self.lineEdit_site_existant.setObjectName("lineEdit_site_existant")

        self.lineEdit = QtGui.QLineEdit(self.groupe_informations)
        self.lineEdit.setGeometry(QtCore.QRect(10,470,311,41))

        font = QtGui.QFont()
        font.setPointSize(14)
        self.lineEdit.setFont(font)
        self.lineEdit.setAlignment(QtCore.Qt.AlignCenter)
        self.lineEdit.setReadOnly(True)
        self.lineEdit.setObjectName("lineEdit")
    
        self.lineEdit_description = QtGui.QLineEdit(self.groupe_informations)
        self.lineEdit_description.setGeometry(QtCore.QRect(10,210,181,41))

        font = QtGui.QFont()
        font.setPointSize(14)
        self.lineEdit_description.setFont(font)
        self.lineEdit_description.setAlignment(QtCore.Qt.AlignCenter)
        self.lineEdit_description.setReadOnly(True)
        self.lineEdit_description.setObjectName("lineEdit_description")

        self.lineEdit_nom = QtGui.QLineEdit(self.groupe_informations)
        self.lineEdit_nom.setGeometry(QtCore.QRect(10,160,91,41))

        font = QtGui.QFont()
        font.setPointSize(14)
        self.lineEdit_nom.setFont(font)
        self.lineEdit_nom.setAlignment(QtCore.Qt.AlignCenter)
        self.lineEdit_nom.setReadOnly(True)
        self.lineEdit_nom.setObjectName("lineEdit_nom")

        self.lineEdit_ref_relative = QtGui.QLineEdit(self.groupe_informations)
        self.lineEdit_ref_relative.setGeometry(QtCore.QRect(10,340,631,41))

        font = QtGui.QFont()
        font.setPointSize(14)
        font.setItalic(False)
        self.lineEdit_ref_relative.setFont(font)
        self.lineEdit_ref_relative.setAutoFillBackground(False)
        self.lineEdit_ref_relative.setAlignment(QtCore.Qt.AlignCenter)
        self.lineEdit_ref_relative.setReadOnly(True)
        self.lineEdit_ref_relative.setObjectName("lineEdit_ref_relative")

        self.lineEdit_longueur_vide = QtGui.QLineEdit(self.groupe_informations)
        self.lineEdit_longueur_vide.setGeometry(QtCore.QRect(330,470,113,41))
        self.lineEdit_longueur_vide.setObjectName("lineEdit_longueur_vide")
	self.lineEdit_longueur_vide.setText("cm")

        self.textEdit_description_vide = QtGui.QTextEdit(self.groupe_informations)
        self.textEdit_description_vide.setGeometry(QtCore.QRect(10,250,631,75))

        font = QtGui.QFont()
        font.setPointSize(12)
        self.textEdit_description_vide.setFont(font)
        self.textEdit_description_vide.setObjectName("textEdit_description_vide")

        self.lineEdit_nom_vide = QtGui.QLineEdit(self.groupe_informations)
        self.lineEdit_nom_vide.setGeometry(QtCore.QRect(100,160,541,41))

        font = QtGui.QFont()
        font.setPointSize(14)
        self.lineEdit_nom_vide.setFont(font)
        self.lineEdit_nom_vide.setObjectName("lineEdit_nom_vide")
      
        self.lineEdit_largeur_vide = QtGui.QLineEdit(self.groupe_informations)
        self.lineEdit_largeur_vide.setGeometry(QtCore.QRect(330,520,113,41))
        self.lineEdit_largeur_vide.setObjectName("lineEdit_largeur_vide")
	self.lineEdit_largeur_vide.setText("cm")

        self.lineEdit_2 = QtGui.QLineEdit(self.groupe_informations)
        self.lineEdit_2.setGeometry(QtCore.QRect(10,520,311,41))

        font = QtGui.QFont()
        font.setPointSize(14)
        self.lineEdit_2.setFont(font)
        self.lineEdit_2.setAlignment(QtCore.Qt.AlignCenter)
        self.lineEdit_2.setReadOnly(True)
        self.lineEdit_2.setObjectName("lineEdit_2")
        

        self.textEdit_ref_relative_vide = QtGui.QTextEdit(self.groupe_informations)
        self.textEdit_ref_relative_vide.setGeometry(QtCore.QRect(10,380,631,81))

        font = QtGui.QFont()
        font.setPointSize(12)
        self.textEdit_ref_relative_vide.setFont(font)
        self.textEdit_ref_relative_vide.setObjectName("textEdit_ref_relative_vide")

        self.lineEdit_new_site = QtGui.QLineEdit(self.groupe_informations)
        self.lineEdit_new_site.setGeometry(QtCore.QRect(10,110,311,41))

        font = QtGui.QFont()
        font.setPointSize(16)
        font.setUnderline(True)
        self.lineEdit_new_site.setFont(font)
        self.lineEdit_new_site.setAlignment(QtCore.Qt.AlignCenter)
        self.lineEdit_new_site.setReadOnly(True)
        self.lineEdit_new_site.setObjectName("lineEdit_new_site")

        self.comboBox_site_existant = QtGui.QComboBox(self.groupe_informations)
        self.comboBox_site_existant.setGeometry(QtCore.QRect(10,50,630,40))
        self.comboBox_site_existant.setObjectName("comboBox_site_existant")
        self.comboBox_site_existant.addItem("...")
        self.comboBox_site_existant.addItem("nouveau batiment")
       
        batiment=Batiment()
        
        self.list=batiment.selectBatimentFromDB()
        for bat in self.list:
             self.comboBox_site_existant.addItem(bat["nom"])
        
      

        self.inser_plan = QtGui.QPushButton(self.groupe_informations)
        self.inser_plan.setGeometry(QtCore.QRect(340,110,301,41))

        font = QtGui.QFont()
        font.setPointSize(14)
        self.inser_plan.setFont(font)
        self.inser_plan.setObjectName("inser_plan")

        self.groupe_image = QtGui.QWidget(Dialog)
        self.groupe_image.setGeometry(QtCore.QRect(670,130,571,511))
        self.groupe_image.setObjectName("groupe_image")

	#cadre autour du plan
        self.frame = QtGui.QFrame(self.groupe_image)
        self.frame.setGeometry(QtCore.QRect(10,10,551,481))
        self.frame.setFrameShape(QtGui.QFrame.StyledPanel)
        self.frame.setFrameShadow(QtGui.QFrame.Raised)
        self.frame.setObjectName("frame")

	self.repere_coord = QtGui.QWidget(self.frame)
	self.repere_coord.setObjectName("repere_coord")
        self.repere_coord.setGeometry(QtCore.QRect(10,10,551,481))

        # label qui contiendra le plan
        self.plan = QtGui.QLabel(self.repere_coord)

        # objet-image
        self.objImg = QtGui.QImage("../images/vide.jpg")
	self.objImgRepere = QtGui.QImage("../images/pointeur1.png")

	# label qui contiendra le repère
	self.repere = QtGui.QLabel(self.repere_coord)
        self.repere.setGeometry(QtCore.QRect(0,0,self.objImgRepere.height(),self.objImgRepere.width()))

        # Initialisation du label
	self.plan.setPixmap(QtGui.QPixmap.fromImage(self.objImg.scaled(530,460,Qt.KeepAspectRatio,Qt.FastTransformation)))
	self.repere.setPixmap(QtGui.QPixmap.fromImage(self.objImgRepere))
	self.repere.setVisible(False)

        self.textEdit_verticale_longueur.setHtml(QtGui.QApplication.translate("Dialog", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'Bitstream Charter\'; font-size:12pt; font-weight:400; font-style:normal;\">\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">L</p>\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">O</p>\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">N</p>\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">G</p>\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">U</p>\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">E</p>\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">U</p>\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">R</p></body></html>", None, QtGui.QApplication.UnicodeUTF8))

        self.textEdit_horizontal_largeur.setHtml(QtGui.QApplication.translate("Dialog", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'Bitstream Charter\'; font-size:12pt; font-weight:400; font-style:normal;\">\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:\'DejaVu Sans\'; font-size:9pt;\"><span style=\" font-family:\'Sans Serif\'; font-size:10pt;\">L A R G E U R</span></p></body></html>", None, QtGui.QApplication.UnicodeUTF8))



	# lien entre les boutons et les actions et/ou appels aux fonctions
	self.retranslateUi(Dialog)
        self.connect(self.inser_plan,QtCore.SIGNAL("clicked()"),self.parcourir)
        self.connect(self.pushButton_suivant,QtCore.SIGNAL("clicked()"),self.suivant)
        self.connect(self.comboBox_site_existant, QtCore.SIGNAL("activated(QString)"), self.displayBatiment)  
        self.connect(self.pushButton_annuler, QtCore.SIGNAL("clicked()"), self.precedent)                       
        QtCore.QMetaObject.connectSlotsByName(Dialog)
   
    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", "Dialog", None, QtGui.QApplication.UnicodeUTF8))
        self.lineEdit_site.setText(QtGui.QApplication.translate("Dialog", "BATIMENT", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton_suivant.setText(QtGui.QApplication.translate("Dialog", "Suivant >>", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton_annuler.setText(QtGui.QApplication.translate("Dialog", "Annuler", None, QtGui.QApplication.UnicodeUTF8))
        self.lineEdit_site_existant.setText(QtGui.QApplication.translate("Dialog", "Batiment Existant", None, QtGui.QApplication.UnicodeUTF8))
        self.lineEdit.setText(QtGui.QApplication.translate("Dialog", "Longueur du batiment :", None, QtGui.QApplication.UnicodeUTF8))
        self.lineEdit_description.setText(QtGui.QApplication.translate("Dialog", "Description :", None, QtGui.QApplication.UnicodeUTF8))
        self.lineEdit_nom.setText(QtGui.QApplication.translate("Dialog", "Nom :", None, QtGui.QApplication.UnicodeUTF8))
        self.lineEdit_ref_relative.setText(QtGui.QApplication.translate("Dialog", "Situation géographique de la référence absolue :", None, QtGui.QApplication.UnicodeUTF8))
        self.lineEdit_2.setText(QtGui.QApplication.translate("Dialog", "Largeur du batiment :", None, QtGui.QApplication.UnicodeUTF8))
        self.lineEdit_new_site.setText(QtGui.QApplication.translate("Dialog", "Nouveau Batiment", None, QtGui.QApplication.UnicodeUTF8))
        self.inser_plan.setText(QtGui.QApplication.translate("Dialog", "Insérer le plan du batiment", None, QtGui.QApplication.UnicodeUTF8))
